<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/commom.js"></script>
    <style>
        
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/博客系统logo.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <img src="/img/搜索 (1).png" id="icon" onclick="searchArt()">
        <input type="search" id="search" placeholder="通过文章标题或文章作者搜索">
        <a href="blog_list.html">主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="login.html">登陆</a>
        <a href="myblog_list.html">我的</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div class="container-right" style="width: 100%;">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <div id="artListDiv">
                
            </div>

            <hr>
            <div class="blog-pagnation-wrapper">
                <button onclick="goFirstPage()" class="blog-pagnation-item">首页</button> 
                <button onclick="goBeforePage()" class="blog-pagnation-item">上一页</button>  
                <button onclick="goAfterPage()" class="blog-pagnation-item">下一页</button>
                <button onclick="goLastPage()" class="blog-pagnation-item">末页</button>
            </div>
        </div>

    </div>

    <script>
         // 当前页码
        var pindex = 1;
        // 每页显示条数
        var psize = 20;
        // 最大页数
        var pcount = 1;
        // 1. 尝试拿到当前url中的页码.
        var page = getUrlValue("pindex");
        pindex = (page == "" ? 1 : page);
        // 2. 查询后端接口得到当前页面数据进行展示.
        function initPage() {
            jQuery.ajax({
                url: "/art/listbypage",
                type: "POST",
                data: {"pindex": pindex, "psize": psize},
                success: function(result) {
                    if (result != null && result.code === 200 && result.data.list.length >= 1) {
                        var artListHtml = ""; // 拼接的变量.
                        for (var i = 0; i < result.data.list.length; i++) {
                            // 循环拿到每一个文章对象.
                            var articleinfo = result.data.list[i];
                            // 开始拼接
                            artListHtml += '<div class="blog">';
                            artListHtml += '<div class="title">'+articleinfo.title+'</div>';
                            artListHtml += '<div class="date">'+articleinfo.updatetime+'</div>';
                            artListHtml += '<div class="desc">'+articleinfo.content+'</div>';
                            artListHtml += '<a href="blog_content.html?id='+ articleinfo.id
                                            +'" class="detail">查看全文 &gt;&gt;</a>';
                            artListHtml += '</div>';
                        }
                        jQuery("#artListDiv").html(artListHtml);
                        pcount = result.data.pcount;
                    }
                }
            });
        }
        initPage();

        // 首页
        function goFirstPage() {
            if (pindex <= 1) {
                // 在首页
                alert("当前已在首页了~")
                return;
            }
            location. href = "blog_list.html";
        }

        // 上一页
        function goBeforePage() {
            // 判断当前是不是在首页.
            if (pindex <= 1) {
                // 在首页
                alert("当前已在首页, 没有上一页了了~")
                return;
            }
            // 不是在首页
            pindex = parseInt(pindex) - 1;
            location.href = "blog_list.html?pindex="+pindex;
        }
        // 下一页
        function goAfterPage() {
            // 判断是不是在末页
            if (pindex >= pcount) {
                alert("当前已在末页, 没有下一页了~")
                return;
            }
            // 不是在末页
            pindex = parseInt(pindex) + 1;
            location.href = "blog_list.html?pindex="+pindex;

        }

        // 末页
        function goLastPage() {
            if (pindex >= pcount) {
                // 在首页
                alert("当前已在末页了~")
                return;
            }
            // 拿到最大页数
            pindex = pcount;
            location.href = "blog_list.html?pindex="+pindex;
        }

        // 跳转到 查询页面
        function searchArt() {
            var search = jQuery("#search");
            // console.log(search.val());
            // alert(search.val())
            if (search.val() === "") {
                return;
            }
            // 搜索框不为空
            location.href = "search_list.html?search=" + search.val();
        }
        
    </script>
</body>
</html>